/*  
 * cafe-timeine.js
 * 预报预警时间轴
 */
;define(function(){
	'use strict';
	function getNodeHtml(cfg){
		if (!cfg.currTime) return;
		$(cfg.appendTo).find('.cafe-busis-timeline').remove();
		var $container = $('<div class="cafe-busis-timeline">');
		
		var currDate = new Date(cfg.currTime);
		var $ul = $('<ul>');
		
		// 实况时间轴
		var $sk = [];
		var currTime = currDate.getTime(), tempDay = currDate.getDate();
		var skInterval = cfg.skInterval, skNum = cfg.skNum;
		if (skInterval) {
			skInterval.forEach(function(interval, i){
				for (var j=0; j<skNum[i]; j++) {
					currTime = currTime - interval * 60 * 1000;
					var tempDate = new Date(currTime);
					if (tempDate.getDate() != tempDay) {
						tempDay = tempDate.getDate();
						var d = new Date(tempDate.getTime() + interval * 60 * 1000);
						$sk.unshift(`<li class=divide>${d.format('dd')}日</li>`);
					}
					$sk.unshift(`<li class=sk title="${tempDate.format('yyyy-MM-dd hh:mm')}"><span></span><i></i><label>${tempDate.format('hh:mm')}</label></li>`);
				}
			})
			if (tempDay == currDate.getDate())
				$sk.unshift(`<li class=divide>${new Date(currTime).format('dd')}日</li>`);
			$sk.push(`<li class='active sk' title="${currDate.format('yyyy-MM-dd hh:mm')}"><span></span><i></i><label>${ currDate.format('hh:mm')}</label></li>`);
		}
		$ul.append($sk);
		
		// 预报时间轴
		var $yb = [];
		var currTime = currDate.getTime(), tempDay = currDate.getDate();
		var ybInterval = cfg.ybInterval, ybNum = cfg.ybNum; 
		if (ybInterval) {
			ybInterval.forEach(function(type, i){
				currTime = calcStartTime(type, currTime);
				var interval = covertMin(type);
				for (var j=0; j<ybNum[i]; j++) {
					currTime = currTime + interval * 60 * 1000;
					var tempDate = new Date(currTime);
					if (tempDate.getDate() != tempDay) {
						tempDay = tempDate.getDate();
						var d = new Date(tempDate.getTime() + interval * 60 * 1000);
						$yb.push(`<li class=divide>${d.format('dd')}日</li>`);
					}
					$yb.push(`<li class=yb title="${tempDate.format('yyyy-MM-dd hh:mm')}"><span></span><i></i><label>${tempDate.format('hh:mm')}</label></li>`);
				}
			})
		}
		$ul.append($yb);
		return $container.append($ul);
	}
	
	// 时间类型转换成分钟数
	function covertMin(type) {
		if (type == 'M6') {
			return 6;
		} else if (type == 'H1') {
			return 1*60;
		} else if (type == 'H3') {
			return 3*60;
		}
	}
	
	// 计算不同类型的开始时间
	function calcStartTime(type, dateTime) {
		var M6 = [0,6,12,18,24,30,36,42,48,54];
		var H3 = [2,5,8,11,14,17,20,23];
		dateTime = new Date(dateTime);
		if (type == 'M6') {
			var min = Number(dateTime.format('mm'));
			for (var i=0; i<M6.length; i++) {
				if (min < M6[i]) {
					dateTime.setMinutes(M6[i-1]);
					break;
				}
			}
		} else if (type == 'H1') {
			var hour = Number(dateTime.format('hh'));
			for (var i=0; i<24; i++) {
				if (hour < i) {
					dateTime.setHours(i-1);
					dateTime.setMinutes(0);
					break;
				}
			}
		} else if (type == 'H3') {
			var hour = Number(dateTime.format('hh'));
			for (var i=0; i<H3.length; i++) {
				if (hour < H3[i]) {
					dateTime.setHours(H3[i-1]);
					dateTime.setMinutes(0);
					break;
				}
			}
		}
		return dateTime.getTime();
	}
	
	// 初始化报警的时间列表
	function buildWarning(cfg, data) {
		$(cfg.appendTo).find(".cafe-busis-timeline > ul > li").each(function(){
			var time = $(this).attr("title");
			if (!time) return;
			if (!data) return;
			var status = data[time];
			if (status == '1') {
				$(this).addClass("warn");
			}
		})
	}
	
	function bindEvent($drop, cfg){
		$drop.find("li label").off('click').on('click', function(event) {
			event.stopPropagation();
			$(this).parent().addClass("active").siblings().removeClass("active");
			var $li = $(this).parent();
			if ($li.hasClass('sk')) {
				if ($.isFunction(cfg.skCall)) 
					cfg.skCall($li.attr("title"));
			}
			if ($li.hasClass('yb')) {
				if ($.isFunction(cfg.ybCall)) 
					cfg.ybCall($li.attr("title"));
			}
		})
		$drop.on('click', 'li.warn i', function(event) {
			event.stopPropagation();
			var $li = $(this).parent();
			if ($li.hasClass('sk')) {
				if ($.isFunction(cfg.skCall)) 
					cfg.iconCall($li.attr("title"));
			}
			if ($li.hasClass('yb')) {}
		})
		
	}
	
	var CFTimeline = function(settings){
		var options = {
				appendTo: '',   // 时间轴所在容器
				data: [],       // 报警数据
				currTime: '',   // 当前时间
				skNum: [],      // 实况时长个数
				skInterval: [], // 实况间隔, 单位:分钟
				ybNum: [],      // 预报时长个数
				ybInterval: [], // 预报间隔, 单位:分钟
				close: null,    // 关闭时间轴事件
				skCall: null,   // 实况点击事件
				ybCall: null,   // 预报点击事件
				iconCall: null, // 预报点击事件
				loadSucc: null
		};
		this.config = $.extend({},options, settings);
		this.$drop = getNodeHtml(this.config);
		if(this.config.appendTo){
			$(this.config.appendTo).append(this.$drop);
		}
		var that = this;
		if ($.isFunction(this.config.loadSucc)) {
			this.config.loadSucc(function(data){
				buildWarning(that.config, data);
			});
		}
		bindEvent(this.$drop, this.config);
	}
	
	CFTimeline.prototype = {
		close: function() {
			this.$drop.hide();
		},
		active: function() {
			this.$drop.find('li.active label').click();
		},
		getCurrTime: function() {
			return this.$drop.find('li.active').attr('title');
		}
	};
	
	CafeJS.Timeline = function(settings){
		var drop =new CFTimeline(settings);
		return drop;
	}
});